/**
 * The QueueApplet is what makes the queue viewable. Contains the panels and buttons in the applet
 * @author Leandro Gallegos
 * @author Alex Garcia
 */
package Queue;

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.TextArea;

import javax.swing.JButton;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class QueueApplet extends Applet implements ActionListener {

	private JButton enqueue, dequeue, first, last, count;
	private JTextField txtfld = new JTextField(20);

	Queue q = Queue.getInstance();
	MyPieChart chart = new MyPieChart();
	TextArea queueVisual = new TextArea(
			"This is where the queue is being displaying", 5, 50);

	/**
	 * This is the init() method that is found in the applet framework it was
	 * overridden to create the GUI
	 */
	public void init() {

		setLayout(new BorderLayout());
		JPanel buttons = new JPanel();
		setBackground(Color.white);
		add(chart, BorderLayout.CENTER);
		buttons.add(queueVisual, BorderLayout.WEST);
		queueVisual.setEditable(false);

		txtfld.setSize(getMaximumSize());
		txtfld.addActionListener(this);

		enqueue = new JButton("Enqueue");
		enqueue.setActionCommand("enqueue");

		dequeue = new JButton("Dequeue");
		dequeue.setActionCommand("dequeue");

		first = new JButton("First");
		first.setActionCommand("first");

		last = new JButton("Last");
		last.setActionCommand("last");

		count = new JButton("Count");
		count.setActionCommand("count");

		add(buttons, BorderLayout.SOUTH);
		setSize(775, 500);

		enqueue.addActionListener(this);
		dequeue.addActionListener(this);
		first.addActionListener(this);
		last.addActionListener(this);
		count.addActionListener(this);

		buttons.add(count, BorderLayout.SOUTH);
		buttons.add(enqueue, BorderLayout.SOUTH);
		buttons.add(dequeue, BorderLayout.SOUTH);
		buttons.add(first, BorderLayout.SOUTH);
		buttons.add(last, BorderLayout.SOUTH);
		add(txtfld, BorderLayout.NORTH);

	}

	/**
	 * This method is what is used to keep the text area updated to show what is
	 * in the queue at the time.
	 */
	public void updateQueueBox() {
		QNode<String> current = q.first;
		String currentQueue = "Current Queue   \n";
		{
			while (current != null) {
				currentQueue += current.getInfo() + "\n";
				current = current.rlink;
			}
		}
		queueVisual.setText(currentQueue);
	}

	@Override
	/**
	 * performs actions based on the buttons pressed
	 * also redraws every time there is a change to the pie chart
	 * @param ActionEvent e The action that is being performed from the buttons found on the applet
	 */
	public void actionPerformed(ActionEvent e) {

		if ("enqueue".equals(e.getActionCommand())) {
			String str = txtfld.getText();
			if (str.equals("")) {
				txtfld.setText("No action taken");
			} else {
				if (q.count > 44) {
					txtfld.setText("The queue is full");
				} else {

					q.enqueue(txtfld.getText());
					if (q.count != 0) {
						if (q.count < 10) {
							chart.slices[q.count % chart.slices.length].color = Color.blue;
						} else if (q.count > 9 && q.count < 19) {
							chart.slices[q.count % chart.slices.length].color = Color.green;
						} else if (q.count > 18 && q.count < 28) {
							chart.slices[q.count % chart.slices.length].color = Color.red;
						} else if (q.count > 27 && q.count < 37) {
							chart.slices[q.count % chart.slices.length].color = Color.orange;
						} else if (q.count > 36 && q.count < 46) {
							chart.slices[q.count % chart.slices.length].color = Color.cyan;
						}

					}
				}
				repaint();

			}

		}
		if ("dequeue".equals(e.getActionCommand())) {
			if (q.count == 0) {
				txtfld.setText("count is 0");
			}
			if (q.count != 0) {
				if (q.count < 10) {
					chart.slices[q.count % chart.slices.length].color = Color.white;
				} else if (q.count > 9 && q.count < 19) {
					chart.slices[q.count % chart.slices.length].color = Color.blue;
				} else if (q.count > 18 && q.count < 28) {
					chart.slices[q.count % chart.slices.length].color = Color.green;
				} else if (q.count > 27 && q.count < 37) {
					chart.slices[q.count % chart.slices.length].color = Color.red;
				} else if (q.count > 36 && q.count < 46) {
					chart.slices[q.count % chart.slices.length].color = Color.orange;
				}
				updateQueueBox();

			}
			q.dequeue();
			repaint();

		}
		if ("first".equals(e.getActionCommand())) {
			txtfld.setText(q.first());
		}
		if ("last".equals(e.getActionCommand())) {
			txtfld.setText(q.last());
		}
		if ("count".equals(e.getActionCommand())) {
			txtfld.setText("" + q.count);
		}
		updateQueueBox();
	}

}